package com.zhima.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class SubTaskExecuteInfo {
    private LocalDateTime successTime;
    private LocalDateTime failTime;
    /**
     * 最新的失败原因
     */
    private String failReason;
    private Integer failCount;

    public static List<SubTaskExecuteInfo> initSubTaskExecuteInfos(int len) {
        List<SubTaskExecuteInfo> ans = new ArrayList<>();
        for (int i = 0; i < len; i++) {
            ans.add(new SubTaskExecuteInfo(null, null, "", 0));
        }
        return ans;
    }

    public void fail(String errorMessage) {
        this.failCount++;
        this.failTime = LocalDateTime.now();
        this.failReason = String.format("%s; %s", this.failReason, errorMessage);
    }

    public void success() {
        this.successTime = LocalDateTime.now();
    }
}
